using System;

namespace Vibstudio.X4NET.Xml.Navigation
{
    internal interface IStringsSeeker
    {
        bool IsExpectedString(string stringToSearch, string searchString);
    }

    internal class SearchTop : IStringsSeeker
    {
        public bool IsExpectedString(string stringToSearch, string searchString)
        {
            return stringToSearch.StartsWith(searchString);
        }
    }

    internal class SearchWithin : IStringsSeeker
    {
        public bool IsExpectedString(string stringToSearch, string searchString)
        {
            return stringToSearch.Contains(searchString);
        }
    }

    internal class SearchToEnd : IStringsSeeker
    {
        public bool IsExpectedString(string stringToSearch, string searchString)
        {
            return stringToSearch.EndsWith(searchString);
        }
    }

    internal class SearchNone : IStringsSeeker
    {
        public bool IsExpectedString(string stringToSearch, string searchString)
        {
            return true;
        }
    }

    public class StringsSeeker
    {
        private readonly IStringsSeeker _seeker;

        public StringsSeeker(SearchCriterion criteria)
        {
            Type type = Type.GetType(string.Format("{0}.Search{1}", GetType().Namespace, criteria));
            _seeker = (IStringsSeeker) Activator.CreateInstance(type);
        }

        public bool IsExpectedString(string stringToSearch, string searchValue)
        {
            return _seeker.IsExpectedString(stringToSearch, searchValue);
        }
    }

    public enum SearchCriterion
    {
        Top, Within, ToEnd, None
    }
}